package com.ruoyi.dataMain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dataMain.domain.bo.DataUploadBo;
import com.ruoyi.dataMain.domain.vo.DataUploadVo;
import com.ruoyi.dataMain.domain.DataUpload;
import com.ruoyi.dataMain.mapper.DataUploadMapper;
import com.ruoyi.dataMain.service.IDataUploadService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 消耗总上传记录Service业务层处理
 *
 * @author linziheng
 * @date 2024-04-21
 */
@RequiredArgsConstructor
@Service
public class DataUploadServiceImpl implements IDataUploadService {

    private final DataUploadMapper baseMapper;

    /**
     * 查询消耗总上传记录
     */
    @Override
    public DataUploadVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询消耗总上传记录列表
     */
    @Override
    public TableDataInfo<DataUploadVo> queryPageList(DataUploadBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataUpload> lqw = buildQueryWrapper(bo);
        Page<DataUploadVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询消耗总上传记录列表
     */
    @Override
    public List<DataUploadVo> queryList(DataUploadBo bo) {
        LambdaQueryWrapper<DataUpload> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataUpload> buildQueryWrapper(DataUploadBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataUpload> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUploadDate()), DataUpload::getUploadDate, bo.getUploadDate());
        lqw.eq(bo.getBitch() != null, DataUpload::getBitch, bo.getBitch());
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), DataUpload::getPath, bo.getPath());
        lqw.orderByDesc(DataUpload::getBitch);
        return lqw;
    }

    /**
     * 新增消耗总上传记录
     */
    @Override
    public Boolean insertByBo(DataUploadBo bo) {
        DataUpload add = BeanUtil.toBean(bo, DataUpload.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改消耗总上传记录
     */
    @Override
    public Boolean updateByBo(DataUploadBo bo) {
        DataUpload update = BeanUtil.toBean(bo, DataUpload.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataUpload entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除消耗总上传记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
